home *** CD-ROM | disk | FTP | other *** search
/ C# & Game Programming - A…er's Guide (2nd Edition) / Buono 2nd Ed.iso / Chapter2 / 2.10 / 2.10.cs next >
Encoding:
Text File  |  2004-08-31  |  1.8 KB  |  42 lines

  1. /* Complex or nested if and if-else statements */
  2. using System;
  3.  
  4. namespace Chapter2 {
  5.     class Class1 {
  6.         static void Main() {
  7.             string Input;
  8.             float Age, Weight;             
  9.             int iQuickAnswer;
  10.      
  11.             // note: you'll want to hit return before entering the second answer        
  12.             Console.Write("How old are you and how much do you weigh? "); 
  13.             Input = Console.ReadLine();
  14.             Age = float.Parse(Input);
  15.             Input = Console.ReadLine();
  16.             Weight = float.Parse(Input);
  17.                        
  18.             if (Age < 3 || Weight < 35) { // start compound if statement     
  19.                 Console.Write("\nThe law requires you to sit in a car seat\n"  
  20.                     + "\n Do you have a car seat? ");
  21.                 iQuickAnswer = Console.Read();     
  22.      
  23.                 if (iQuickAnswer == 'y') // nested if statement
  24.                     Console.Write("\n Good, but using it would be better.\n");
  25.                 else { // nested else compounded    
  26.                     Console.Write("\n No, do you care about your baby?\n");    
  27.                     if (iQuickAnswer == 'y') // nested if in nested else
  28.                         Console.Write("\n Well then get a car seat!\n");
  29.                     else if (iQuickAnswer == 'n') // nested else-if in nested else
  30.                         Console.Write("\n You sicken me!\n" 
  31.                             + "\n Your baby is worth it!\n");
  32.                     else // nested else in nested else
  33.                         Console.Write("\n Your baby is worth it!\n");
  34.                 }                               
  35.             } // end compound if statement
  36.             else if (Age > 85 || Weight > 500)
  37.                 Console.WriteLine("\n Sorry I asked!\n");
  38.         }
  39.     }
  40. }
  41.  
  42.